package cn.ronghuanet.org.controller;

import cn.ronghuanet.auth.annotation.RonghuaPermission;
import cn.ronghuanet.basic.util.AjaxResult;
import cn.ronghuanet.basic.util.PageList;
import cn.ronghuanet.org.domain.Department;
import cn.ronghuanet.org.query.DepartmentQuery;
import cn.ronghuanet.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 从现在开始,后端的controller建议都用@RestController
 * 因为前后端分离结构,后端只需要提供数据就可以了,不需要响应页面,既然如此,所以我们在类上就使用@RestController
 */
@RestController
@RequestMapping("/department")
@Api(value = "部门管理",description = "部门管理的描述")
@RonghuaPermission(name = "部门管理",desc = "部门管理的权限")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 新增/修改接口
     * @param department
     */
    /**
     * 表明接口的请求方式
     * 方式一:@RequestMapping(method = RequestMethod.PUT)
     * @RequestMapping(method = RequestMethod.PUT) // 表明当前是一个PUT请求方式
     * 方式二:@PutMapping
     * 对于前后端分离来说,增删改操作要告知前端一下,当前这个操作是否成功,如果操作不成功,要返回不成功的原因是什么
     * 所以增删改操作,要返回给前端两个数据  success:true/false 来表明操作是成功还是失败  message:返回的信息(特别是失败,原因是一定要返回的)
     * 怎么返回?
     * 方式一: 字符串拼接 return "{\"success\":false,\"message\":\"\"}"; 拼接起来麻烦,而且很容易出错
     * 方式二: Map 通过Map对象返回  在返回时框架底层会帮我们自动将Map对象转成JSON格式的字符串返回  但是写起来也比较麻烦
     * 方式三: 自定义一个返回值对象 AjaxResult  框架底层也会自动帮我们把对象转成JSON格式的字符串返回
     *
     * PUT/POST请求 在接收对象作为参数时,要用@RequestBody
     * @param department
     */
    // 在启动时可以去扫描所有打了@RonghuaPermission的方法,就可以去封装Permission对象 直接保存到数据库
    @ApiOperation(value = "新增/修改部门")
    @PutMapping     // 等价于@RequestMapping(method = RequestMethod.PUT)
    @RonghuaPermission(name = "新增/修改",desc = "新增修改部门")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if(Objects.nonNull(department.getId())){    // ID不为空,说明是修改操作
                departmentService.update(department);
            }else{
                // ID为空,说明是新增操作
                departmentService.add(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存失败!"+e.getMessage());
        }
    }

    // 删除
    @ApiOperation(value = "删除部门")
    @DeleteMapping("/{id}")
    @RonghuaPermission(name = "删除部门",desc = "删除部门")
    public AjaxResult deleteById(@ApiParam(value = "主键",required = true)@PathVariable("id")Long id){
        try {
            departmentService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败!"+e.getMessage());
        }
    }

    // 根据ID进行查询
    /**
     * 查询接口返回时,不能确定它一定是成功的,所以也要返回AjaxResult,但是查询出来的数据也要返回
     * @return
     */
    @ApiOperation(value = "根据ID查询部门")
    @GetMapping("/{id}")
    @RonghuaPermission(name = "根据ID查询部门",desc = "根据ID查询部门")
    public AjaxResult getById(@ApiParam(value = "主键",required = true) @PathVariable("id")Long id){
        try {
            Department department = departmentService.getById(id);
            return AjaxResult.me().setResultObj(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败!"+e.getMessage());
        }
    }

    /**
     * 查询所有
     * @return
     */
    @ApiOperation(value = "查询所有")
    @GetMapping
    @RonghuaPermission(name = "查询所有",desc = "查询所有")
    public AjaxResult getAll(){
        try {
            List<Department> departments = departmentService.getAll();
            return AjaxResult.me().setResultObj(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败!"+e.getMessage());
        }
    }

    // 分页查询
    @PostMapping
    @RonghuaPermission(name = "分页查询",desc = "分页查询")
    public AjaxResult pageList(@RequestBody DepartmentQuery query){
        try {
            PageList<Department> list = departmentService.pageList(query);
            return AjaxResult.me().setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("分页查询失败!"+e.getMessage());
        }
    }

    // 批量删除
    @PatchMapping
    @RonghuaPermission(name = "批量删除",desc = "批量删除")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            departmentService.batchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败!"+e.getMessage());
        }
    }

    // 查询一级部门和它的二级部门
    @GetMapping("/tree")
    public AjaxResult tree(){
        try {
            List<Department> list = departmentService.getTree();
            return AjaxResult.me().setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("部门树查询失败!"+e.getMessage());
        }
    }
}
